package com.tarena.lbs.message.web.service;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.common.message.constance.Const;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.message.web.repository.MessageRepository;
import com.tarena.lbs.message.web.repository.TriggerTemplateRepository;
import com.tarena.lbs.pojo.message.dos.TriggerMsgTemplate;
import com.tarena.lbs.pojo.message.event.TriggerMsgEvent;
import com.tarena.lbs.pojo.message.param.TriggerMsgTemplateInsertParam;
import com.tarena.lbs.pojo.message.po.MessagePO;
import com.tarena.lbs.pojo.message.po.TriggerTemplatePO;
import com.tarena.lbs.pojo.message.vo.TriggerTemplateVO;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class TriggerService {
    @Autowired
    private TriggerTemplateRepository triggerTemplateRepository;
    @Autowired
    private MessageRepository messageRepository;
    public List<TriggerTemplateVO> selectTriggers() {
        List<TriggerTemplatePO> pos=triggerTemplateRepository.getAllTriggerTemplates();
        List<TriggerTemplateVO> vos=null;
        if (CollectionUtils.isNotEmpty(pos)){
            vos=pos.stream().map(po->{
                TriggerTemplateVO vo=new TriggerTemplateVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return vos;
    }

    public void triggerTemplateSave(TriggerMsgTemplateInsertParam param) throws BusinessException {
        log.info("新增触发模版入参:{}",param);
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","用户认证失败"));
        TriggerTemplatePO poParam=new TriggerTemplatePO();
        BeanUtils.copyProperties(param,poParam);
        poParam.setBussinessId(userPrinciple.getId());
        poParam.setCreateAt(new Date());
        triggerTemplateRepository.saveTriggerMsgTemplate(poParam);
    }

    public void triggerMsg(TriggerMsgEvent event) {
        log.info("获取的event:{}",event);
        //先查询当前行为是否有模版
        String actionName = event.getActionName();
        //同一类行模版只能有一个
        TriggerTemplatePO po=triggerTemplateRepository.getTriggerTemplatesByUserAction(actionName);
        if (po!=null){
            //如果没有结束 如果有 根据模版 填补event数据生成message存储到数据库
            MessagePO messagePO=new MessagePO();
            messagePO.setActivityId(event.getActivityId());
            messagePO.setCreateAt(new Date());
            messagePO.setStatus(0);
            String msgTitle=generateTitle(po,event);
            String content=generateContent(po,event);
            messagePO.setTemplateId(po.getId());
            messagePO.setContent(content);
            messagePO.setMsgTitle(msgTitle);
            messagePO.setMsgType(event.getSource());
            //谁该看到 谁是message用户 这里肯定是被点赞的人
            messagePO.setUserId(event.getAriticleUserId());
            log.info("写入的消息:{}",messagePO);
            messageRepository.saveMessage(messagePO);
        }

    }

    private String generateContent(TriggerTemplatePO po,TriggerMsgEvent event) {
        String content = po.getContent();
        if (content.contains(Const.USER_NAME)||content.contains(Const.ARTICLE_TITLE)||content.contains(Const.LIKED_USER_NAME)){
            content = content.replace(Const.USER_NAME,event.getUsername());
            content = content.replace(Const.LIKED_USER_NAME,event.getArticleUsername());
            content = content.replace(Const.ARTICLE_TITLE,event.getArticleTitle());
        }
        return content;
    }

    private String generateTitle(TriggerTemplatePO po,TriggerMsgEvent event) {
        String msgTitle = po.getMsgTitle();
        if (msgTitle.contains(Const.USER_NAME)||msgTitle.contains(Const.ARTICLE_TITLE)||msgTitle.contains(Const.LIKED_USER_NAME)){
            msgTitle = msgTitle.replace(Const.USER_NAME,event.getUsername());
            msgTitle = msgTitle.replace(Const.LIKED_USER_NAME,event.getArticleUsername());
            msgTitle = msgTitle.replace(Const.ARTICLE_TITLE,event.getArticleTitle());
        }
        return msgTitle;
    }
}
